/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qq.vip.mikuPy2001.bukkit;

import com.qq.vip.mikuPy2001.bukkit.gui.DefaultViewListener;
import com.qq.vip.mikuPy2001.bukkit.gui.model.Button;
import com.qq.vip.mikuPy2001.bukkit.gui.model.EditBox;
import com.qq.vip.mikuPy2001.bukkit.gui.model.Module;
import com.qq.vip.mikuPy2001.bukkit.gui.util.MyJavaPlugin;
import com.qq.vip.mikuPy2001.bukkit.gui.Windows;
import com.qq.vip.mikuPy2001.bukkit.gui.WindowsView;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import static com.qq.vip.mikuPy2001.bukkit.gui.util.ItemUtil.newItem;

/**
 *
 * @author MukiPy2001
 */
public class GUI extends MyJavaPlugin {

    @Override
    public void onEnable() {
        getDataFolder().mkdirs();
    }

    @Override
    public void onDisable() {
        super.onDisable();
        Windows.close();
    }

    private Windows win1, win2;

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            if (args.length > 0 && args[0].equalsIgnoreCase("con")) {
                if (win2 == null) {
                    win2 = Windows.loadFromConfiguration(getConfig())
                            .addAllListener(new DefaultViewListenerImpl());
                }
                win2.open((Player) sender);
            } else {
                if (win1 == null)
                    win1 = new Windows("&c测试用&2界面")
                            .setModule(new Button("STONE", 2, 1, newItem(Material.STONE, "&2点击我显示石头"), true))
                            .setModule(new Button("WOOD", 2, 9, newItem(Material.WOOD, "&2点击我显示木头"), true))
                            .setModule(new Button("noshow", 1, 5, newItem(Material.BED, "&2火把和石头都在睡觉哦"), true))
                            .setModule(new Button("STONE_show", 1, 5, newItem(Material.STONE, "&2我是石头,遵循召唤而来")))
                            .setModule(new Button("WOOD_show", 1, 5, newItem(Material.WOOD, "&2我是木头,遵循召唤而来")))
                            .setModule(new EditBox("EditBox", 2, 5))
                            .addAllListener(new DefaultViewListenerImpl());
                win1.open((Player) sender);
            }
            return true;
        } else {
            return false;
        }
    }

    private static class DefaultViewListenerImpl extends DefaultViewListener {

        @Override
        public void onClick(WindowsView wv, Module module) {
            switch (module.getModuleID()) {
                case "STONE":
                    wv.enable("STONE_show");
                    break;
                case "WOOD":
                    wv.enable("WOOD_show");
                    break;
            }
        }
    }
}
